<?php
namespace Task\Core;

use Task;

class Base
{
    protected $db;         //数据库对象	//采用medoo数据库类，来源：http://medoo.lvtao.net/index.php
    protected $log;        //日志对象
    private $allowIps;    //允许执行的ip地址
    private $clientIp;    //访问的ip

    public function __construct()
    {
        //声明日志对象
        $this->log = CLog::getInstance();

        //自动加载初始化函数
        if (method_exists($this, '_initialize')) {
            $this->_initialize();
        }
    }

    /**
     * 获取数据库对象
     * @param $type
     * @return  object
     */
    public function selectDB($type)
    {
        return Database::getInstance($type);
    }

    /**
     *
     * 初始化方法
     */
    protected function _initialize()
    {

    }

    /**
     * 自动检测ip
     */
    protected function autoChkIp()
    {
        $this->clientIp = $this->getClientIp();
        $this->allowIps = $this->getAllowIp();
        $this->checkIp();
    }

    /**
     *
     * 检测ip
     */
    private function checkIp()
    {
        //判断如果允许访问的ip是字符串，那么就截取成数组
        if (is_string($this->allowIps)) {
            $this->allowIps = explode(',', $this->allowIps);
        }

        $cnt = count($this->allowIps);
        if (!$cnt) {
            exit('-10000');  //没有设置允许访问的ip
        }

        if ($this->clientIp && !in_array($this->clientIp, $this->allowIps)) {
            exit('-10001');   //IP非法
        }
    }

    /**
     *
     * 获得表名
     * @param string $tableName 表名
     */
    protected function getTable($tableName)
    {
        return config('db_prefix') . $tableName;
    }

    /**
     *
     * 获得数量
     * @param string $condition
     * @return int 数量
     */
    protected function getCount($condition = '')
    {
    }

    /**
     *
     * 获得列表数据，用来做分页,往往是大批量数据写操作，需要分批写入
     * @param string $condition 查询条件
     * @param int $page 第几页
     * @return array 数组
     */
    protected function getList($page, $condition = '')
    {

    }

    /**
     * 获取客户端IP地址
     * @return mixed
     */
    private function getClientIp()
    {
        $realip = null;
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $realip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $realip = $_SERVER['REMOTE_ADDR'];
            } elseif (isset($_SERVER['SERVER_ADDR'])) {
                $realip = $_SERVER['SERVER_ADDR'];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } elseif (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } elseif (getenv("REMOTE_ADDR")) {
                $realip = getenv("REMOTE_ADDR");
            } elseif (getenv("SERVER_ADDR")) {
                $realip = getenv("SERVER_ADDR");
            }
        }
        return $realip;
    }

    /**
     *
     * 设置允许访问的ip
     * @param array $allowIps
     */
    public function setAllowIp($allowIps)
    {
        $this->allowIps = $allowIps;
    }

    public function getAllowIp()
    {
        return $this->allowIps;
    }

    //声明缓存类
    public function cache($type, $redisDbNum = 0)
    {
        $type = strtolower($type);

        if ($type == 'redis') {
            $host = config('REDIS_HOST') ? config('REDIS_HOST') : '182.92.71.70';
            $port = config('REDIS_PORT') ? config('REDIS_PORT') : 6379;
            $timeout = config('REDIS_CACHE_TIMEOUT') ? config('REDIS_CACHE_TIMEOUT') : 0.0;
            $redis = new \Redis();
            $redis->connect($host, $port, $timeout);

            if (!$redis->auth(config('REDIS_PASS'))) {
                return false;
            }

            $redisDbNum = $redisDbNum ?: config('REDIS_DEF_DB');
            $redis->select($redisDbNum);

            return $redis;
        } else {
//            $cache = new Cache();
//            return $cache->connect($type);
            return null;
        }
    }

    public function __destruct()
    {

    }
}